package com.youdu.vuandroidadsdk.okhttp.request;

import java.util.Map;

import okhttp3.FormBody;
import okhttp3.Request;

/**
 * @Title:CommonRequest
 * @Package:com.youdu.vuandroidadsdk.okhttp.request
 * @Description:接收请求参数，并生成一个request对象
 * @Auther:YJH
 * @Email:yuannunhua@gmail.com
 * @Date:2018/7/2312:49
 */
public class CommonRequest {

    /**
     * 功能：接收参数，返回一个post请求方式的request对象；
     *
     * @param url
     * @param params
     * @return
     */
    public static Request createPostRequest(String url, RequestParams params) {
        FormBody.Builder mFormBody = new FormBody.Builder();
        if (null != params) {
            for (Map.Entry<String, String> entry : params.urlParams.entrySet()) {
                mFormBody.add(entry.getKey(), entry.getValue());
            }

        }

        FormBody formBody = mFormBody.build();

        return new Request.Builder().url(url).post(formBody).build();
    }

    /**
     * 功能：接收参数，返回一个get请求方式的request对象；
     *
     * @param url
     * @param params
     * @return
     */
    public static Request createGetRequest(String url, RequestParams params){
        StringBuilder sb = new StringBuilder().append(url).append("?");
        if(null != params){
            for (Map.Entry<String, String> entry : params.urlParams.entrySet()) {
                sb.append(entry.getKey()).append("=").append(entry.getKey()).append("&");
            }
        }

        String strUrl = sb.toString();
        return new Request.Builder().url(strUrl.substring(0, strUrl.length()-1)).get().build();
    }


}
